package com.yxzz.core.service.impl;

import com.ruoyi.common.core.utils.StringUtils;
import com.yxzz.core.domain.PHealthHistory;
import com.yxzz.core.domain.PPatient;
import com.yxzz.core.mapper.PPatientMapper;
import com.yxzz.core.service.IPPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 病人信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-28
 */
@Service
public class PPatientServiceImpl implements IPPatientService 
{
    @Autowired
    private PPatientMapper pPatientMapper;

    /**
     * 查询病人信息
     * 
     * @param id 病人信息主键
     * @return 病人信息
     */
    @Override
    public PPatient selectPPatientById(Long id)
    {
        return pPatientMapper.selectPPatientById(id);
    }

    /**
     * 查询病人信息列表
     * 
     * @param pPatient 病人信息
     * @return 病人信息
     */
    @Override
    public List<PPatient> selectPPatientList(PPatient pPatient)
    {
        return pPatientMapper.selectPPatientList(pPatient);
    }

    /**
     * 新增病人信息
     * 
     * @param pPatient 病人信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPPatient(PPatient pPatient)
    {
        int rows = pPatientMapper.insertPPatient(pPatient);
        insertPHealthHistory(pPatient);
        return rows;
    }

    /**
     * 修改病人信息
     * 
     * @param pPatient 病人信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePPatient(PPatient pPatient)
    {
        pPatientMapper.deletePHealthHistoryByPatientId(pPatient.getId());
        insertPHealthHistory(pPatient);
        return pPatientMapper.updatePPatient(pPatient);
    }

    /**
     * 批量删除病人信息
     * 
     * @param ids 需要删除的病人信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePPatientByIds(Long[] ids)
    {
        pPatientMapper.deletePHealthHistoryByPatientIds(ids);
        return pPatientMapper.deletePPatientByIds(ids);
    }

    /**
     * 删除病人信息信息
     * 
     * @param id 病人信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePPatientById(Long id)
    {
        pPatientMapper.deletePHealthHistoryByPatientId(id);
        return pPatientMapper.deletePPatientById(id);
    }

    @Override
    public List<PPatient> selectPPatientListByIds(Long[] ids) {
        return pPatientMapper.selectPPatientListByIds(ids);
    }

    /**
     * 新增病人健康史信息
     * 
     * @param pPatient 病人信息对象
     */
    public void insertPHealthHistory(PPatient pPatient)
    {
        List<PHealthHistory> pHealthHistoryList = pPatient.getPHealthHistoryList();
        Long id = pPatient.getId();
        if (StringUtils.isNotNull(pHealthHistoryList))
        {
            List<PHealthHistory> list = new ArrayList<PHealthHistory>();
            for (PHealthHistory pHealthHistory : pHealthHistoryList)
            {
                pHealthHistory.setPatientId(id);
                list.add(pHealthHistory);
            }
            if (list.size() > 0)
            {
                pPatientMapper.batchPHealthHistory(list);
            }
        }
    }
}
